﻿CREATE PROCEDURE [dbo].[proc_Order_Change_OrderItem_Create]
	(
		@OId uniqueidentifier
		,@CId	int
		,@ProductId	int
		,@ProductName	nvarchar(50)
		,@UnitPrice	decimal(18, 2)
		,@Quantity int
		,@SalesmanAccount varchar(20)
		,@CreateDate datetime
		,@Description nvarchar(200)	
	)
AS
Begin
	If @SalesmanAccount Is Null Or @UnitPrice Is Null Or @Quantity Is Null
		Return

	Declare @DepId int,@CompanyId int,@Salesman nvarchar(50),@je decimal(18, 2)
	Select @Salesman=EName,@DepId=DepartmentId,@CompanyId=CompanyId From Employee Where UserName=@SalesmanAccount
	If @DepId Is Null Or @CompanyId Is Null
		Return

	Update [Order_Change] Set flag=1 Where OId=@OId And Id=@CId And flag=0
	Insert into Order_Change_Item
	(
		OId,CId
		,ProductId
		,ProductName
		,UnitPrice
		,Quantity
		,Salesman
		,SalesmanAccount
		,DepId
		,CompanyId
		,CreateDate
		,Description
		,TradeDate
	)
	Values
	(
		@OId,@CId
		,@ProductId
		,@ProductName
		,@UnitPrice
		,@Quantity
		,@Salesman
		,@SalesmanAccount
		,@DepId
		,@CompanyId
		,@CreateDate
		,@Description
		,@CreateDate
	)
End